import { createClient } from 'redis';

class RedisClient {
  private client: any;

  constructor() {
    this.client = createClient({
      url: process.env.REDIS_URL || 'redis://localhost:6379'
    });
    this.client.on('error', (err: any) => console.error('Redis Client Error', err));
    this.connect();
  }

  async connect() {
    await this.client.connect();
  }

   async get(key: string): Promise<string | null> {
    return this.client.get(key);
  }

  async set(key: string, value: string, options?: { EX: number }): Promise<void> {
    await this.client.set(key, value, options);
  }
}

const redisClient = new RedisClient();

// 明确导出实例和方法
module.exports = {
    redisClient,
    get: redisClient.get.bind(redisClient),
    set: redisClient.set.bind(redisClient)
};
